package com.ccdc.lbcmsystem.controller;


import com.ccdc.lbcmsystem.mapper.ChildrenMapper;
import com.ccdc.lbcmsystem.mapper.ExamineMapper;
import com.ccdc.lbcmsystem.mapper.UserMapper;
import com.ccdc.lbcmsystem.mapper.VeteranMapper;
import com.ccdc.lbcmsystem.pojo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class VeteranController {

    @Autowired
    VeteranMapper veteranMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ExamineMapper examineMapper;

    @RequestMapping("/veteranList")
    public String veteranList(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                               @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(startPage,pageSize);
        List<Veteran> veterans = veteranMapper.queryVeteranList();
        PageInfo<Veteran> veteranPageInfo = new PageInfo<>(veterans);
        System.out.println(veteranPageInfo.getPageNum());
        model.addAttribute("veteranPageInfo", veteranPageInfo);
        model.addAttribute("veterans",veterans);
        return "/veteran/veteranList";
    }

    @RequestMapping("/searchVeteran")
    public String searchVeteran(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                 @RequestParam(defaultValue = "10") Integer pageSize, String name, HttpSession session){
        PageHelper.startPage(startPage, pageSize);
        List<Veteran> veterans = veteranMapper.queryListVeteranByName(name);
        PageInfo<Veteran> veteransPageInfo = new PageInfo<>(veterans);
        session.setAttribute("name",name);
        model.addAttribute("veteransPageInfo", veteransPageInfo);
        model.addAttribute("veterans",veterans);
        return "veteran/veteranSearchList";
    }


    @RequestMapping("/toVeteranAdd")
    public String toVeteranAdd(){
        return "veteran/veteranAdd";
    }

    @RequestMapping("/veteranAdd")
    @ResponseBody
    public String veteranAdd(Veteran veteran) {
        String msg = "";
        SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime = new Date();
        String time = sft.format(createTime);
        veteran.setCreateTime(time);
        veteran.setDeleteStatus(0);
        try {
            veteranMapper.addVeteran(veteran);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "新增失败";
            return msg;
        }

    }

    @RequestMapping("/veteranLookInfo/{id}")
    public String veteranLookInfo(@PathVariable("id") Integer id, Model model){
        Veteran veteran = veteranMapper.queryVeteranById(id);
        model.addAttribute("veteran",veteran);
        return "veteran/veteranLookInfo";
    }


    @RequestMapping("/toVeteranEdit/{id}")
    public String toVeteranEdit(@PathVariable("id") Integer id, Model model){
        Veteran veteran = veteranMapper.queryVeteranById(id);
        model.addAttribute("veteran",veteran);
        return "veteran/veteranEdit";
    }

    @RequestMapping("/veteranEdit")
    @ResponseBody
    public String veteranEdit(String name,Integer id,Integer age,String phone,String birthday,String army,String address
                               ,String healthCondition,Integer health
            ,Integer familyStatus){
        String msg = "";
        try{
            veteranMapper.updateVeteranById(id,name, phone,birthday, age,army,address
                    ,healthCondition, health,familyStatus);
            msg = "OK";
            return msg;
        }catch (Exception e){
            System.out.println(e);
            msg = "修改失败";
            return msg;
        }

    }

    @RequestMapping("/veteranDelete")
    @ResponseBody
    public String veteranDelete(Integer id, Model model, String examineUser, Delveteran delveteran, String reason){
        String msg = "";

        System.out.println(examineUser);
        try {
            User user = userMapper.queryUserByUserName(examineUser);
            Veteran veteran = veteranMapper.queryVeteranById(id);
            System.out.println(user);
            //工作人员删除 需要提交给管理员审核
            if (user.getRole().equals("4")){
                Integer deleteStatus = 2;//将删除状态码修改成 2   提交给管理员审核
                veteranMapper.delVeteranById(id,deleteStatus);
                Delveteran delveteran1 = examineMapper.queryDelveteran(id);
                if (delveteran1 != null){
                    msg = "该退伍军人信息已提交删除申请,请勿重复操作!";
                    return msg;
                }else {
                    delveteran.setDelUser(examineUser);
                    delveteran.setId(veteran.getId());
                    delveteran.setName(veteran.getName());
                    delveteran.setPhone(veteran.getPhone());
                    delveteran.setBirthday(veteran.getBirthday());
                    delveteran.setAge(veteran.getAge());
                    delveteran.setArmy(veteran.getArmy());
                    delveteran.setAddress(veteran.getAddress());
                    delveteran.setHealth(veteran.getHealth());
                    delveteran.setHealthCondition(veteran.getHealthCondition());
                    delveteran.setFamilyStatus(veteran.getFamilyStatus());
                    delveteran.setDeleteStatus(2);
                    delveteran.setReason(reason);
                    SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date createTime = new Date();
                    String time = sft.format(createTime);
                    delveteran.setCreateTime(time);
                    examineMapper.addDelVeteran(delveteran);
                    msg = "OK";
                    return msg;
                }
            }else {
                Integer deleteStatus = 1;
                veteranMapper.delVeteranById(id,deleteStatus);
                msg = "OK";
                return msg;
            }

        }catch (Exception e){
            msg = "删除失败";
            return msg;
        }
    }
}
